from selenium.webdriver.chrome.options import Options
from selenium.webdriver import Chrome
import os
from core import spider


class Param:
    """
    :param url:
    :param ua:
    :return: url opt
    """

    def __init__(self, url, ua):
        self.url = url
        self.ua = ua

    def parameter(self):
        opt = Options()
        opt.add_experimental_option('excludeSwitches', ['enable-automation'])
        opt.add_argument('--headless')
        opt.add_argument('user-agent={}'.format(self.ua))
        url = self.url
        return url, opt


if __name__ == '__main__':
    if not os.path.exists('./Abstract'):
        os.mkdir('./Abstract')

    url = 'https://www.cnki.net/?pageId=77825&wfwfid=145305&websiteId=58201'
    ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko)' \
         ' Chrome/92.0.4515.131 Safari/537.36'

    # 初始化
    p = Param(url, ua)
    url, opt = p.parameter()

    web = Chrome(options=opt)
    web.get(url)
    # while True:
    spider(web)
    print("爬取完毕1")
    # choice = input('是否搜索下一个关键字（是/否）：')
    # if choice == '否':
    #     break
